package com.net.xpay.web.controller.merchant;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.xpay.web.vo.AlipayUserVo;
import com.net.common.exception.BusinessException;
import com.net.common.model.ResultData;
import com.net.common.model.ResultList;
import com.net.common.model.ResultPagedList;
import com.net.xpay.common.domain.User;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.domain.AlipayUser;
import com.net.xpay.core.enums.AlipayUserStatus;
import com.net.xpay.core.manager.AlipayUserManager;
import com.net.xpay.core.model.AlipayUserSearchModel;
import com.net.xpay.core.model.AlipayUserUpdateModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import com.net.xpay.web.secutiry.RequestUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**

 */
@RestController
@Slf4j
@RequestMapping("/merchant/alipay-user")
@Api(tags = "商户/支付宝账号", description = "商户 支付宝账号 net")
public class AlipayUserController {

    @Autowired
    private AlipayUserManager alipayUserManager;

    /**
     * 分页
     */
    @ApiOperation("分页")
    @PostMapping("/search")
    public ResultPagedList<AlipayUserVo> search(@ApiIgnore @RequestUser User merchant, @RequestBody AlipayUserSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        searchModel.setUserId(merchant.getId());
        searchModel.setIsDeleted(0);
        List<AlipayUser> records = alipayUserManager.search(searchModel);
        if (CollectionUtils.isEmpty(records)) {
            searchModel.setAlipayUserId(searchModel.getAlipayNickName());
            searchModel.setAlipayNickName(null);
            records = alipayUserManager.search(searchModel);
        }
        List<AlipayUserVo> vos = BeanCopierUtil.batchCopy(AlipayUser.class, AlipayUserVo.class, records);
        long count = alipayUserManager.searchCount(searchModel);

        return new ResultPagedList<>(vos, count, searchModel);
    }

    /**
     * 列表
     */
    @ApiOperation("列表")
    @PostMapping("/list")
    public ResultList<AlipayUserVo> list(@ApiIgnore @RequestUser User merchant, @RequestBody AlipayUserSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        searchModel.setUserId(merchant.getId());
        searchModel.setIsDeleted(0);
        List<AlipayUser> records = alipayUserManager.list(searchModel);
        List<AlipayUserVo> vos = BeanCopierUtil.batchCopy(AlipayUser.class, AlipayUserVo.class, records);

        return new ResultList<>(vos);
    }

    /**
     * 删除支付宝账号
     */
    @ApiOperation("删除支付宝账号")
    @PostMapping("/delete")
    public ResultData<Boolean> deleteAlipayUser(@ApiIgnore @RequestUser User merchant, Long id) {
        Preconditions.checkArgument(id != null);
        AlipayUser alipayUser = alipayUserManager.findById(merchant.getId(), id);
        if (alipayUser == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "支付宝账户不存在");
        }

        alipayUser.setIsDeleted(1);
        alipayUserManager.updateByPrimaryKeyWithVersion(alipayUser);

        return new ResultData<>(true);
    }
    /**
     * 解约支付宝账号
     */
    @ApiOperation("解约支付宝账号")
    @PostMapping("/unsign")
    public ResultData<Boolean> unsignAlipayUser(@ApiIgnore @RequestUser User merchant, Long id) {
        Preconditions.checkArgument(id != null);
        AlipayUser alipayUser = alipayUserManager.findById(merchant.getId(), id);
        if (alipayUser == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "支付宝账户不存在");
        }

        alipayUser.setStatus(AlipayUserStatus.NO);
        alipayUserManager.updateByPrimaryKeyWithVersion(alipayUser);

        return new ResultData<>(true);
    }

    /**
     * 修改备注
     */
    @ApiOperation("修改备注")
    @PutMapping("/remark")
    public ResultData<AlipayUserVo> updateRemark(@ApiIgnore @RequestUser User merchant, @RequestBody AlipayUserUpdateModel updateModel) {
        Preconditions.checkArgument(updateModel != null && updateModel.getId() != null);
        if (Strings.isNullOrEmpty(updateModel.getRemark())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "备注不能为空");
        }
        if (updateModel.getRemark().length() > 16) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "备注最多16个字");
        }

        AlipayUser alipayUser = alipayUserManager.findById(merchant.getId(), updateModel.getId());
        if (alipayUser == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "支付宝账户不存在");
        }

        alipayUser.setRemark(updateModel.getRemark());
        alipayUserManager.updateByPrimaryKeyWithVersion(alipayUser);
        AlipayUserVo vo = new AlipayUserVo();
        BeanCopierUtil.copy(alipayUser, vo);
        return new ResultData<>(vo);
    }

}
